﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Diagnostics;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.Customization;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DTO;
using BUS;

namespace GUI
{
    public partial class NhanLichChuyenBay_GUI : DevExpress.XtraEditors.XtraUserControl
    {
        public NhanLichChuyenBay_GUI()
        {
            InitializeComponent();
        }

        BindingList<NhanLichChuyenBay_DTO> listNhanLichChuyenBay = NhanLichChuyenBay_BUS.GetNhanLichChuyenBayList();
        BindingList<ChiTietChuyenBay_DTO> listChiTietRong = new BindingList<ChiTietChuyenBay_DTO>();
        BindingList<ChiTietChuyenBay_DTO> listChiTiet = new BindingList<ChiTietChuyenBay_DTO>();
        BindingList<ChuyenBay_DTO> listChuyenBay = ChuyenBay_BUS.GetChuyenBayList();
        List<SanBay_DTO> listSanBay = SanBay_BUS.GetSanBayList();
        List<TinhTrangVe_DTO> listTinhTrang = new List<TinhTrangVe_DTO>();
        List<DonGiaVe_DTO> listDonGia = new List<DonGiaVe_DTO>();
        ThamSo_DTO thamso = ThamSo_BUS.GetThamSoDetails();

        private void NhanLichChuyenBay_GUI_Load(object sender, EventArgs e)
        {
            gridC_DSChuyenBay.DataSource = listNhanLichChuyenBay;
            gridC_ChiTietChuyenBay.DataSource = listChiTietRong;
            
            cmbSanBayTrungGian.ValueMember = SanBayColumns.MaSanBay;
            cmbSanBayTrungGian.DisplayMember = SanBayColumns.TenSanBay;
            cmbSanBayTrungGian.DataSource = listSanBay;

            SanBayDi.ValueMember = SanBayColumns.MaSanBay;
            SanBayDi.DisplayMember = SanBayColumns.TenSanBay;
            SanBayDi.DataSource = listSanBay;

            SanBayDen.ValueMember = SanBayColumns.MaSanBay;
            SanBayDen.DisplayMember = SanBayColumns.TenSanBay;
            SanBayDen.DataSource = listSanBay;
        }

        private void advB_DSChuyenBay_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                //flag_CT = 0;
                btnCreate_CT.Text = "Lập Chi Tiết";

                NhanLichChuyenBay_DTO DTO = (NhanLichChuyenBay_DTO)advB_DSChuyenBay.GetFocusedRow();
                // Show Chi Tiết Chuyến Bay
                string MaChuyenBay = DTO.ChuyenBay;
                listChiTiet = ChiTietChuyenBay_BUS.GetChiTietChuyenBayDetails(MaChuyenBay);
                gridC_ChiTietChuyenBay.DataSource = listChiTiet;
            }
        }

        string CreateMaChuyenBay()
        {
            string Kq = "CB0";
            char[] c = "CB0".ToCharArray();
            if (listChuyenBay.Count == 0)
            {
                Kq = "CB001";
            }
            else
            {
                string Ma = ChuyenBay_BUS.MaxMaChuyenBay();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }
        string CreateMaTuyenBay()
        {
            string Kq = "TB0";
            char[] c = "TB0".ToCharArray();
            List<TuyenBay_DTO> listTuyenBay = TuyenBay_BUS.GetTuyenBayList();
            if (listTuyenBay.Count == 0)
            {
                Kq = "TB001";
            }
            else
            {
                string Ma = TuyenBay_BUS.MaxMaTuyenBay();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }
        string CreateMaTinhTrangVe()
        {
            string Kq = "TT0";
            char[] c = "TT0".ToCharArray();
            List<TinhTrangVe_DTO> listTinhTrangVe = TinhTrangVe_BUS.GetTinhTrangVeList();
            if (listTinhTrangVe.Count == 0)
            {
                Kq = "TT001";
            }
            else
            {
                string Ma = TinhTrangVe_BUS.MaxTinhTrangVe();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }
        string CreateMaChiTiet()
        {
            string Kq = "CT0";
            char[] c = "CT0".ToCharArray();
            List<ChiTietChuyenBay_DTO> listChiTiet = ChiTietChuyenBay_BUS.GetChiTietChuyenBayList();
            if (listChiTiet.Count == 0)
            {
                Kq = "CT001";
            }
            else
            {
                string Ma = ChiTietChuyenBay_BUS.MaxMaChiTietChuyenBay();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }

        int KiemTra(string Ma, int a, int b)
        {
            int flag = 1;
            int temp;
            int trong;
            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    TinhTrangVe_DTO tinhtrangDTO1 = TinhTrangVe_BUS.GetTinhTrangVeDetails(Ma, "HV001");
                    temp = tinhtrangDTO1.SoGheTrong + tinhtrangDTO1.SoGheDat;
                    if (a < temp)
                    {
                        trong = temp - a;
                        if (trong > tinhtrangDTO1.SoGheTrong)
                        {
                            flag = 0;
                        }
                    }
                }
                if (i == 1)
                {
                    TinhTrangVe_DTO tinhtrangDTO2 = TinhTrangVe_BUS.GetTinhTrangVeDetails(Ma, "HV002");
                    temp = tinhtrangDTO2.SoGheTrong + tinhtrangDTO2.SoGheDat;
                    if (b < temp)
                    {
                        trong = temp - b;
                        if (trong > tinhtrangDTO2.SoGheTrong)
                        {
                            flag = 0;
                        }
                    }
                }
            }
            return flag;
        }
        void UpdateTinhTrang(NhanLichChuyenBay_DTO DTO)
        {
             int flag = KiemTra(DTO.ChuyenBay, DTO.SoLuongGheHang1, DTO.SoLuongGheHang2);
                if (flag == 1)
                {
                    int temp;
                    TinhTrangVe_DTO tinhtrangveDTO = new TinhTrangVe_DTO();
                    for (int i = 0; i < 2; i++)
                    {
                        if (i == 0)
                        {
                            tinhtrangveDTO = TinhTrangVe_BUS.GetTinhTrangVeDetails(DTO.ChuyenBay, "HV001");
                            temp = tinhtrangveDTO.SoGheTrong + tinhtrangveDTO.SoGheDat;
                            if (DTO.SoLuongGheHang1 > temp)
                            {
                                tinhtrangveDTO.SoGheTrong = tinhtrangveDTO.SoGheTrong + (DTO.SoLuongGheHang1 - temp);
                            }
                            else
                            {
                                tinhtrangveDTO.SoGheTrong = tinhtrangveDTO.SoGheTrong - (temp - DTO.SoLuongGheHang1);
                            }
                        }
                        if (i == 1)
                        {
                            tinhtrangveDTO = TinhTrangVe_BUS.GetTinhTrangVeDetails(DTO.ChuyenBay, "HV002");
                            temp = tinhtrangveDTO.SoGheTrong + tinhtrangveDTO.SoGheDat;
                            if (DTO.SoLuongGheHang2 > temp)
                            {
                                tinhtrangveDTO.SoGheTrong = tinhtrangveDTO.SoGheTrong + (DTO.SoLuongGheHang2 - temp);
                            }
                            else
                            {
                                tinhtrangveDTO.SoGheTrong = tinhtrangveDTO.SoGheTrong - (temp - DTO.SoLuongGheHang2);
                            }
                        }
                        TinhTrangVe_BUS.UpdateTinhTrangVe(tinhtrangveDTO);
                    }
                }
        }
        void UpdateDonGiaVe(NhanLichChuyenBay_DTO DTO)
        {
            DonGiaVe_DTO dongiaDTO = new DonGiaVe_DTO();
            for (int i = 0; i < 2; i++)
            {
                dongiaDTO.MaTuyenBay = DTO.TuyenBay;
                if (i == 0)
                {
                    dongiaDTO = DonGiaVe_BUS.GetDonGiaDetails(DTO.TuyenBay, "HV001");
                    dongiaDTO.MaDonGia = dongiaDTO.MaDonGia;
                    dongiaDTO.MaHangVe = "HV001";
                    dongiaDTO.DonGia = DTO.DonGiaVe1;
                }
                if (i == 1)
                {
                    dongiaDTO = DonGiaVe_BUS.GetDonGiaDetails(DTO.TuyenBay, "HV002");
                    dongiaDTO.MaDonGia = dongiaDTO.MaDonGia;
                    dongiaDTO.MaHangVe = "HV002";
                    dongiaDTO.DonGia = DTO.DonGiaVe2;
                }
                DonGiaVe_BUS.UpdateDonGiaVe(dongiaDTO);
            }
        }
        void UpdateChiTiet(ChuyenBay_DTO chuyenbayDTO)
        {
            for (int i = 0; i < listChiTiet.Count; i++)
            {
                ChiTietChuyenBay_BUS.UpdateChiTietChuyenBay(listChiTiet[i]);
            }
            listChiTiet = ChiTietChuyenBay_BUS.GetChiTietChuyenBayDetails(chuyenbayDTO.MaChuyenBay);
            gridC_ChiTietChuyenBay.DataSource = listChiTiet;
        }

        private void advB_DSChuyenBay_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView view = sender as GridView;
            if ((view.FocusedColumn.FieldName == "ChuyenBay") || (view.FocusedColumn.FieldName == "STT"))
                e.Cancel = true;
        }
        private void advB_DSChuyenBay_RowUpdated(object sender, RowObjectEventArgs e)
        {
            NhanLichChuyenBay_DTO row = (NhanLichChuyenBay_DTO)advB_DSChuyenBay.GetFocusedRow();
            // Update Tuyến Bay
            TuyenBay_DTO tuyenbayDTO = new TuyenBay_DTO();
            tuyenbayDTO.MaTuyenBay = row.TuyenBay;
            tuyenbayDTO.SanBayDi = row.SanBayDi;
            tuyenbayDTO.SanBayDen = row.SanBayDen;
            TuyenBay_BUS.UpdateTuyenBay(tuyenbayDTO);
            // Update Chuyến Bay
            ChuyenBay_DTO chuyenbay = new ChuyenBay_DTO();
            chuyenbay.MaChuyenBay = row.ChuyenBay;
            chuyenbay.MaTuyenBay = tuyenbayDTO.MaTuyenBay;
            chuyenbay.Ngay_Gio = row.NgayGio;
            chuyenbay.ThoiGianBay = row.ThoiGianBay;
            chuyenbay.SoLuongGheHang1 = row.SoLuongGheHang1;
            chuyenbay.SoLuongGheHang2 = row.SoLuongGheHang2;
            ChuyenBay_BUS.UpdateChuyenBay(chuyenbay);
            // Update Đơn Giá Vé
            UpdateDonGiaVe(row);
            // Update Tình Trạng Vé
            UpdateTinhTrang(row);
        }
        private void advB_DSChuyenBay_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            string MaChuyenBay = CreateMaChuyenBay();
            int STT = advB_DSChuyenBay.RowCount;
            advB_DSChuyenBay.SetRowCellValue(e.RowHandle, advB_DSChuyenBay.Columns[0], STT);
            advB_DSChuyenBay.SetRowCellValue(e.RowHandle, advB_DSChuyenBay.Columns[1], MaChuyenBay);
            advB_DSChuyenBay.SetRowCellValue(e.RowHandle, advB_DSChuyenBay.Columns["NgayGio"], DateTime.Today);
            advB_DSChuyenBay.SetRowCellValue(e.RowHandle, advB_DSChuyenBay.Columns["ThoiGianBay"], thamso.ThoiGianBayToiThieu);
        }
        private void advB_DSChuyenBay_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            ColumnView view = sender as ColumnView;
            GridColumn column3 = view.Columns["ThoiGianBay"];
            GridColumn column4 = view.Columns["SanBayDi"];
            GridColumn column5 = view.Columns["SanBayDen"];
            GridColumn column6 = view.Columns["SoLuongGheHang1"];
            GridColumn column7 = view.Columns["DonGiaVe1"];
            GridColumn column8 = view.Columns["SoLuongGheHang2"];
            GridColumn column9 = view.Columns["DonGiaVe2"];
            int txtThoiGian = (int)view.GetRowCellValue(e.RowHandle, column3);
            string txtSanBayDi = (string)view.GetRowCellValue(e.RowHandle, column4);
            string txtSanBayDen = (string)view.GetRowCellValue(e.RowHandle, column5);
            int txtSoLuong1 = (int)view.GetRowCellValue(e.RowHandle, column6);
            float txtDonGia1 = (float)view.GetRowCellValue(e.RowHandle, column7);
            int txtSoLuong2 = (int)view.GetRowCellValue(e.RowHandle, column8);
            float txtDonGia2 = (float)view.GetRowCellValue(e.RowHandle, column9);
            if (Convert.ToInt32(txtThoiGian) < thamso.ThoiGianBayToiThieu)
            {
                e.Valid = false;
                view.SetColumnError(column3, "Thời Gian Bay Tối Thiểu " + thamso.ThoiGianBayToiThieu);
            }
            if (txtSanBayDi == null)
            {
                e.Valid = false;
                view.SetColumnError(column4, "Chưa Chọn Sân Bay ĐI");
            }
            if (txtSanBayDen == null)
            {
                e.Valid = false;
                view.SetColumnError(column5, "Chưa Chọn Sân Bay Đến");
            }
            if (Convert.ToInt32(txtSoLuong1) == 0)
            {
                e.Valid = false;
                view.SetColumnError(column6, "Chưa Nhập Số Lượng Ghế Hạng 1");
            }
            if (Convert.ToInt32(txtDonGia1) == 0)
            {
                e.Valid = false;
                view.SetColumnError(column7, "Chưa Nhập Đơn Giá Cho Ghế Hạng 1");
            }
            if (Convert.ToInt32(txtSoLuong2) == 0)
            {
                e.Valid = false;
                view.SetColumnError(column8, "Chưa Nhập Số Lượng Ghế Hạng 2");
            }
            if (Convert.ToInt32(txtDonGia2) == 0)
            {
                e.Valid = false;
                view.SetColumnError(column9, "Chưa Nhập Đơn Giá Cho Ghế Hạng 2");
            }
        }
        private void advB_DSChuyenBay_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }

        int flag = 0;
        //int flag_CT = 0;
        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                listChiTietRong.Clear();
                gridC_ChiTietChuyenBay.DataSource = null;
                advB_DSChuyenBay.AddNewRow();
                gridC_ChiTietChuyenBay.DataSource = listChiTietRong;

                flag = 1;
                btnDelete.Enabled = false;
                btnInsert.Text = "Tiếp Nhận";
            }
            else
            {
                NhanLichChuyenBay_DTO row = (NhanLichChuyenBay_DTO)advB_DSChuyenBay.GetFocusedRow();
                // Insert Tuyến Bay
                TuyenBay_DTO tuyenbayDTO = new TuyenBay_DTO();
                tuyenbayDTO.MaTuyenBay = CreateMaTuyenBay();
                tuyenbayDTO.SanBayDi = row.SanBayDi;
                tuyenbayDTO.SanBayDen = row.SanBayDen;
                TuyenBay_BUS.InsertTuyenBay(tuyenbayDTO);
                // Insert Chuyến Bay
                ChuyenBay_DTO chuyenbay = new ChuyenBay_DTO();
                chuyenbay.MaChuyenBay = row.ChuyenBay;
                chuyenbay.MaTuyenBay = tuyenbayDTO.MaTuyenBay;
                chuyenbay.Ngay_Gio = row.NgayGio;
                chuyenbay.ThoiGianBay = row.ThoiGianBay;
                chuyenbay.SoLuongGheHang1 = row.SoLuongGheHang1;
                chuyenbay.SoLuongGheHang2 = row.SoLuongGheHang2;
                ChuyenBay_BUS.InsertChuyenBay(chuyenbay);
                // Insert Đơn Giá Vé
                for (int i = 0; i < 2; i++)
                {
                    DonGiaVe_DTO DTO = new DonGiaVe_DTO();
                    DTO.MaTuyenBay = tuyenbayDTO.MaTuyenBay;
                    if (i == 0)
                    {
                        DTO.MaHangVe = "HV001";
                        DTO.DonGia = row.DonGiaVe1;
                    }
                    if (i == 1)
                    {
                        DTO.MaHangVe = "HV002";
                        DTO.DonGia = row.DonGiaVe2;
                    }
                    DonGiaVe_BUS.InsertDonGiaVe(DTO);
                }
                // Insert Tình Trạng Vé
                for (int i = 0; i < 2; i++)
                {
                    TinhTrangVe_DTO tinhtrangveDTO = new TinhTrangVe_DTO();
                    tinhtrangveDTO.MaTinhTrangVe = CreateMaTinhTrangVe();
                    tinhtrangveDTO.MaChuyenBay = row.ChuyenBay;
                    if (i == 0)
                    {
                        tinhtrangveDTO.MaHangVe = "HV001";
                        tinhtrangveDTO.SoGheTrong = row.SoLuongGheHang1;
                    }
                    if (i == 1)
                    {
                        tinhtrangveDTO.MaHangVe = "HV002";
                        tinhtrangveDTO.SoGheTrong = row.SoLuongGheHang2;
                    }
                    tinhtrangveDTO.SoGheDat = 0;
                    TinhTrangVe_BUS.InsertTinhTrangVe(tinhtrangveDTO);
                }
                // Insert Chi Tiết
                for (int i = 0; i < listChiTietRong.Count; i++)
                {
                    listChiTietRong[i].MaChiTietChuyenBay = CreateMaChiTiet();
                    listChiTietRong[i].MaChuyenBay = row.ChuyenBay;
                    ChiTietChuyenBay_BUS.InsertChiTietChuyenBay(listChiTietRong[i]);
                }

                listChiTiet = ChiTietChuyenBay_BUS.GetChiTietChuyenBayDetails(row.ChuyenBay);
                gridC_ChiTietChuyenBay.DataSource = listChiTiet;

                listNhanLichChuyenBay = NhanLichChuyenBay_BUS.GetNhanLichChuyenBayList();
                gridC_DSChuyenBay.DataSource = listNhanLichChuyenBay;

                btnDelete.Enabled = true;
                btnInsert.Text = "Lập Chuyến Bay";
                flag = 0;
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Muốn Huỷ ?", "Thông Báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                NhanLichChuyenBay_DTO row = (NhanLichChuyenBay_DTO)advB_DSChuyenBay.GetFocusedRow();
                // Delete Chi Tiết
                listChiTiet = ChiTietChuyenBay_BUS.GetChiTietChuyenBayDetails(row.ChuyenBay);
                foreach (ChiTietChuyenBay_DTO item in listChiTiet)
                {
                    ChiTietChuyenBay_BUS.DeleteChiTietChuyenBay(item);
                }
                gridC_ChiTietChuyenBay.DataSource = null;
                // Delete Tình Trang
                listTinhTrang = TinhTrangVe_BUS.GetTinhTrangDetails(row.ChuyenBay);
                for (int i = 0; i < listTinhTrang.Count; i++)
                {
                    TinhTrangVe_BUS.DeleteTinhTrangVe(listTinhTrang[i]);
                }
                // Delete Đơn Giá
                listDonGia = DonGiaVe_BUS.GetDonGiaVeDetails(row.TuyenBay);
                for (int i = 0; i < listDonGia.Count; i++)
                {
                    DonGiaVe_BUS.DeleteDonGiaVe(listDonGia[i]);
                }
                // Delete Chuyến Bay
                ChuyenBay_DTO chuyenbay = new ChuyenBay_DTO();
                chuyenbay.MaChuyenBay = row.ChuyenBay;
                ChuyenBay_BUS.DeleteChuyenBay(chuyenbay);
                // Delete Tuyến Bay
                TuyenBay_DTO tuyenbay = new TuyenBay_DTO();
                tuyenbay.MaTuyenBay = row.TuyenBay;
                //tuyenbay.SanBayDi = row.SanBayDi;
                //tuyenbay.SanBayDen = row.SanBayDen;
                TuyenBay_BUS.DeleteTuyenBay(tuyenbay);

                listNhanLichChuyenBay = NhanLichChuyenBay_BUS.GetNhanLichChuyenBayList();
                gridC_DSChuyenBay.DataSource = listNhanLichChuyenBay;
                //gridV_DSChuyenBay.DeleteRow(listChuyenBay.Count - 1);
                MessageBox.Show("Bạn Đã Hủy Xong", "..:: Thông Báo ::..");
            }
        }

        private void gridV_ChiTietChuyenBay_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView view = sender as GridView;
            if (view.FocusedColumn.FieldName == "STT")
                e.Cancel = true;
        }
        private void gridV_ChiTietChuyenBay_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            //string MaChiTiet = CreateMaChiTiet();
            //if (flag == 0)
            //{
                int STT = gridV_ChiTietChuyenBay.RowCount;
                gridV_ChiTietChuyenBay.SetRowCellValue(e.RowHandle, gridV_ChiTietChuyenBay.Columns[0], STT);
                gridV_ChiTietChuyenBay.SetRowCellValue(e.RowHandle, gridV_ChiTietChuyenBay.Columns[2], thamso.ThoiGianDungToiThieu);
                //gridV_ChiTietChuyenBay.SetRowCellValue(e.RowHandle, gridV_ChiTietChuyenBay.Columns[1], MaChiTiet);
            //}
            //else
            //{
            //    int STT = gridV_ChiTietChuyenBay.RowCount;
            //    gridV_ChiTietChuyenBay.SetRowCellValue(e.RowHandle, gridV_ChiTietChuyenBay.Columns[0], STT);
            //    gridV_ChiTietChuyenBay.SetRowCellValue(e.RowHandle, gridV_ChiTietChuyenBay.Columns[2], thamso.ThoiGianDungToiThieu);
            //    //gridV_ChiTietChuyenBay.SetRowCellValue(e.RowHandle, gridV_ChiTietChuyenBay.Columns[1], MaChiTiet);
            //}
        }
        private void gridV_ChiTietChuyenBay_RowUpdated(object sender, RowObjectEventArgs e)
        {
            ChiTietChuyenBay_DTO chitietDTO = (ChiTietChuyenBay_DTO)gridV_ChiTietChuyenBay.GetFocusedRow();
            ChiTietChuyenBay_BUS.UpdateChiTietChuyenBay(chitietDTO);
        }
        private void gridV_ChiTietChuyenBay_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }
        private void gridV_ChiTietChuyenBay_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            ColumnView view = sender as ColumnView;
            GridColumn column1 = view.Columns[1];
            GridColumn column2 = view.Columns[2];
            string txtSanBay = (string)view.GetRowCellValue(e.RowHandle, column1);
            int txtThoiGian = (int)view.GetRowCellValue(e.RowHandle, column2);
            if(txtSanBay == null)
            {
                e.Valid = false;
                view.SetColumnError(column1, "Chưa Chọn Sân Bay Dừng");
            }
            else
            {
                if ((Convert.ToInt32(txtThoiGian) < thamso.ThoiGianDungToiThieu) || (Convert.ToInt32(txtThoiGian) > thamso.ThoiGianDungToiDa))
                {
                    e.Valid = false;
                    view.SetColumnError(column2, "Thời Gian Dừng (" + thamso.ThoiGianDungToiThieu + "-->" + thamso.ThoiGianDungToiDa + ")");
                }
            }
        }

        private void btnCreate_CT_Click(object sender, EventArgs e)
        {
            if (flag == 1)
            {
                if (gridV_ChiTietChuyenBay.RowCount < thamso.SoSanBayTrungGianToiDa)
                {
                    gridV_ChiTietChuyenBay.AddNewRow();
                }
                else
                {
                    MessageBox.Show("Số Sân Bay Trung Gian Tối Đa Là " + thamso.SoSanBayTrungGianToiDa, "..:: Thông Báo ::..");
                }
            }
            else
            {
                if (gridV_ChiTietChuyenBay.RowCount < thamso.SoSanBayTrungGianToiDa)
                {
                    gridV_ChiTietChuyenBay.AddNewRow();
                    NhanLichChuyenBay_DTO DTO = (NhanLichChuyenBay_DTO)advB_DSChuyenBay.GetFocusedRow();
                    ChiTietChuyenBay_DTO row = (ChiTietChuyenBay_DTO)gridV_ChiTietChuyenBay.GetFocusedRow();
                    row.MaChiTietChuyenBay = CreateMaChiTiet();
                    row.MaChuyenBay = DTO.ChuyenBay;
                    ChiTietChuyenBay_BUS.InsertChiTietChuyenBay(row);
                }
                else
                {
                    MessageBox.Show("Số Sân Bay Trung Gian Tối Đa Là " + thamso.SoSanBayTrungGianToiDa, "..:: Thông Báo ::..");
                }
            }
        }
        private void btnDelete_CT_Click(object sender, EventArgs e)
        {
            ChiTietChuyenBay_DTO item = (ChiTietChuyenBay_DTO)gridV_ChiTietChuyenBay.GetFocusedRow();
            ChiTietChuyenBay_BUS.DeleteChiTietChuyenBay(item);

            listChiTiet = ChiTietChuyenBay_BUS.GetChiTietChuyenBayDetails(item.MaChuyenBay);
            gridC_ChiTietChuyenBay.DataSource = listChiTiet;
        }

        private void btnXuatEX_Click(object sender, EventArgs e)
        {
            GridView view = gridC_DSChuyenBay.MainView as GridView;
            if (view != null)
            {
                //view.ExportToXlsx("DanhSachChuyenBay.xlsx");
                view.ExportToPdf("DanhSachChuyenBay.pdf");
                Process pdfExport = new Process();
                pdfExport.StartInfo.FileName = "AcroRd32.exe";
                pdfExport.StartInfo.Arguments = "DanhSachChuyenBay.pdf";
                pdfExport.Start();
            }
        }
    }
}
